<template>
  <div class="login">
    <h2 class="title">登录</h2>
    <!--输入框 -->
    <el-form
      ref="form"
      :model="form"
      label-width="60px"
      class="form"
      :rules="rules"
    >
      <el-form-item label="账号" prop="username">
        <el-input
          prefix-icon="el-icon-user-solid"
          v-model="form.username"
          placeholder="请输入账号"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          placeholder="请输入密码"
          v-model="form.password"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">登录</el-button>
        <el-button @click="resetBtn('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { userLoginAPI } from "../apis/user";
import { setToken, setUserId } from "../utils/locaData";
export default {
  data() {
    return {
      form: {
        username: "13226231118",
        password: "123",
      },
      //表单校验规则
      rules: {
        //可以写在一起共用一个提示 或者分开写
        username: [
          //如果输入框为空
          //required必填属性 message错误的提示信息 trigger触发时机
          { required: true, message: "请输入手机号码", trigger: "blur" },
          {
            // pattern配置正则检查
            pattern: /^1\d{10}$|^\d{5,6}$/,
            message: "请输入正确的手机号码",
            trigger: "blur",
          },
        ],
        password: [
          //min max控制长度
          {
            required: true,
            min: 3,
            max: 11,
            message: "请输入3到18位密码",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //点击通知按钮
    onSubmit() {
      //检测所有表单都通过验证规则
      this.$refs.form.validate(async (valid) => {
        // console.log("是否成功", valid);
        if (valid) {
          //发送请求
          const res = await userLoginAPI(this.form);
          console.log(res);
          if (res.data.message === "登录成功") {
            //存入token
            setToken(res.data.data.token);
            setUserId(res.data.data.user.id);
            this.$message({
              showClose: true,
              message: "恭喜你，登录成功",
              type: "success",
            });
            //跳转到首页
            this.$router.push({ path: "./home" });
          } else {
            this.$message({
              showClose: true,
              message: "登录失败,用户名或密码错误",
              type: "error",
            });
          }
        } else {
          this.$message({
            showClose: true,
            message: "登录失败,用户名或密码错误",
            type: "error",
          });
          console.log("error submit!!");
          return false;
        }
      });
    },
    //重置按钮
    resetBtn() {
      this.$refs.form.resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  background-color: #f2f2f2;
  height: 100vh;
  display: flex;
  align-items: center;
  flex-direction: column;
  padding-top: 10vh;
  box-sizing: border-box;
  .title {
    font-size: 30px;
    margin: 20px;
  }
  .form {
    background-color: #fff;
    width: 400px;
    padding: 30px;
    border-radius: 10px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
  }
}
</style>
